<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="收费日期" class="w-[300px] mr-5">
          <a-range-picker v-model:value="formState['dateRange']" format="YYYY-MM-DD" value-format="YYYY-MM-DD" @change="getTollCollectorData()" />
        </a-form-item>
        <a-form-item name="pyjm" label="" class="w-[300px]">
          <a-input v-model:value="formState.pyjm" placeholder="住院号/姓名/助记码" />
        </a-form-item>
        <a-form-item name="pjhm" label="" class="w-[200px] mx-2">
          <a-input v-model:value="formState.pjhm" placeholder="票据号" />
        </a-form-item>
        <a-form-item name="isTf" label="显示退费" class="w-[100px]">
          <a-switch v-model:checked="formState.isTf" un-checked-value="0" checked-value="KQ" @change="getData" />
        </a-form-item>
        <a-form-item label="收费员" name="czy" class="w-[260px] ml-2">
          <a-select v-model:value="formState.czy" mode="multiple" :max-tag-count="2" placeholder="请选择" :options="wardData" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-2">
        <BaseExport type="住院" name="收费检索" code="000739" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable fill :loading="loading" column-code="000739" :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record.status === 0" color="blue">在院</a-tag>
            <a-tag v-if="record.status === 1" color="green">已出院</a-tag>
            <a-tag v-if="record.status === 2" color="orange">待出院</a-tag>
            <a-tag v-if="record.status === 3" color="purple">待入区</a-tag>
            <a-tag v-if="record.status === 4" color="red">作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickPrint(record, 1)"> 结算清单 </a-button>
            <a-button type="link" size="small" @click="onClickPrint(record, 2)"> 住院清单 </a-button>
            <a-button type="link" size="small" @click="onPrintMedical(record)">报销单</a-button>
            <TableMoreButton>
              <a-button type="link" size="small" @click="onClickPrint(record, 3)"> 发票 </a-button>
            </TableMoreButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
// import { post } from '@/utils/request'

const formRef = ref(null)
const wardData = ref([])
const formState = reactive({
  dateRange: [dayjs().subtract(6, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  czy: []
})
const { loading, pagination, tableData, getData } = useTable({
  url: '/inp/inpComprehensive/getInpTollRetrievalList',
  formatParams: () => {
    return {
      stdate: formState.dateRange?.[0],
      eddate: formState.dateRange?.[1],
      pyjm: formState?.pyjm,
      pjhm: formState?.pjhm,
      czy: formState?.czy,
      isTf: formState?.isTf
    }
  }
})

// 收费员
const getTollCollectorData = async () => {
  const res = await post('/outp/mzdj/getTollCollectors', {
    stdate: formState['dateRange']?.[0],
    eddate: formState['dateRange']?.[1]
  })
  wardData.value = res.data.map((item) => {
    return {
      label: item?.empName,
      value: item?.empId
    }
  })
}

const onClickPrint = async (row, status) => {
  if (status === 1) {
    const { code, msg, data } = await post('/inp/Print/printSettlementDocumentsList', {
      inplsh: row?.inplsh
    })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  }

  if (status === 2) {
    const { code, msg, data } = await post('/inp/Print/printAdmissionList', {
      inplsh: row?.inplsh,
      jslb: 'SF',
      dylb: 'ZY'
    })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  }

  if (status === 3) {
    const { code, data, msg } = await post('/inp/Print/printZYFP', {
      inplsh: row?.inplsh,
      pjhm: row?.pjhm
    })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  }
}
const onPrintMedical = async (row) => {
  const { code, msg, data } = await post('/inp/Print/printFeeStatements', { inplsh: row?.inplsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}

onMounted(() => {
  getTollCollectorData()
  getData()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
